<?php
/**
 * User: @lose
 * Date: 2019-10-10
 * Time: 18:02
 */

namespace Biz\service\admin;

use Biz\model\store\StoreAccount as mAccount;
use Biz\model\store\StoreOrder as mOrder;
use Doraemon\tools\IdParse;

class StoreOrder
{
    public  static function getListByWhere($where , $limit)
    {
      $list =   mOrder::getOrderListByWhere($where , $limit);
      if($list && is_array($list)){
          foreach($list as &$val){
               $val['server_type'] = mAccount::SERVER_TYPE_MAP[$val['server_type']];
               $val['payment_method'] = mOrder::TRANSFER_METHOD[$val['payment_method']];
          }
      }
      $count = mOrder::getOrderCountByWhere($where);
      return ['list' => $list , 'count' => $count];

    }

    public static function updateOrder($data , $id)
    {
        return  mOrder::updateById($id , $data);
    }

    public static function InsertOrder($data)
    {
        return mOrder::add($data);
    }

    public static function getOrderByAId($account_id)
    {
        $order_info = [];
        if($account_id){
            $order_info = mOrder::getInfoByAid($account_id);
        }
        $account_info = mAccount::getInfo($account_id);
        if($account_info && is_array($account_info)){
            $order_info['store_id'] = $account_info['store_id'];
            $order_info['uid']  = $account_info['uid'];
            $order_info['store_name'] = $account_info['store_name'];
            $order_info['auth_end_time'] = $order_info['auth_end_time'] ?: date("Ymd" , strtotime("+1 year"));;
            $order_info['auth_start_time'] = $order_info['auth_start_time'] ?: date("Ymd" , time());

        }

        return $order_info;
    }

    /*生成订单编号
     *
     *
     * */
    public static function createOrderNo($account_id , $uid)
    {
        $microTime = bcmul(microtime(true), 1000);
        $microTime = sprintf('%s%s', date('His'), substr($microTime, -4, 3));
        $microTime = strtoupper(base_convert($microTime, 10, 32));
        $time = date('ymd');
        $parseId = strtoupper(base_convert($account_id, 10, 32));
        $hashUid = IdParse::hashUid($uid);
        $order = [
            $time,
            $microTime,
            $parseId,
            $hashUid
        ];
        $orderNo = implode('-', $order);
        return $orderNo;

    }





}